import xlrd
import time
from docx import Document
from docx.oxml.ns import qn
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Inches, Pt
from datetime import datetime, timedelta
from tkinter import *
import tkinter.filedialog

root = Tk()
filename = tkinter.filedialog.askopenfilename(title='选择一个txt文件', filetypes=[('xlsx', '*.xlsx'), ('All Files', '*')],
                                              initialdir='C:\\Windows\\WsdlFile')
root.destroy()  # 和mainloop()配合可以关闭tk空白窗口
root.mainloop()  #
if filename == '':
    print("error")
else:
    print("filename" + filename)


def date(dates):  # 定义转化日期戳的函数,dates为日期戳
    delta = timedelta(days=dates)
    today = datetime.strptime('1899-12-30', '%Y-%m-%d') + delta  # 将1899-12-30转化为可以计算的时间格式并加上要转化的日期戳
    return datetime.strftime(today, '%Y-%m-%d')  # 制定输出日期的格式


# sheet_name = input("请输入sheet名称：")
xls = xlrd.open_workbook(filename)
# sheet = xls.sheet_by_index(1)
sheet = xls.sheet_by_name("离职名单")
all_data = []
num_set = set()
for row_i in range(1, sheet.nrows):
    name = sheet.cell_value(row_i, 2)
    sex = sheet.cell_value(row_i, 3)
    title = sheet.cell_value(row_i, 13)
    id_card = sheet.cell_value(row_i, 38)
    dateon = sheet.cell_value(row_i, 20)
    dateout = sheet.cell_value(row_i, 57)
    dateon_new = date(dateon)
    dateout_new = date(dateout)
    ZJ = {
        '人名': name,
        '性别': sex,
        '职位': title,
        '身份证号码': id_card,
        '入职时间': dateon_new,
        '离职时间': dateout_new
    }
    all_data.append(ZJ)
    # print(all_data[row_i][0])
    # num_set.add(name)
print(all_data)
# print(len(all_data))
# print(len(num_set))

personal_info = []
value = list
ersonal_info = []
found = False
# name1 = input('轻松输入需要开离职证明的姓名：')
while not found:
    name1 = input('轻松输入需要开离职证明的姓名：')
    for ZJ in all_data:
        if name1 in ZJ['人名']:
            _info = {
                '人名': ZJ['人名'],
                '性别': ZJ['性别'],
                '职位': ZJ['职位'],
                '身份证号码': ZJ['身份证号码'],
                '入职日期': ZJ['入职时间'],
                '离职日期': ZJ['离职时间']
            }
            value = list(_info.values())
            found = True
            break
# personal_info.append(value)
print(value)

# print(personal_info[0])
# _staff: str = input('请输入员工姓名:')

document = Document()
document.styles['Normal'].font.name = u'思源黑体 CN Normal'
document.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'), u'思源黑体 CN Normal')

p = document.add_paragraph()
p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run = p.add_run('离职证明')
run.font.size = Pt(21)
run.font.bold = True
# document.add_heading('离职证明', 0)
p1 = document.add_paragraph()
p1 = document.add_paragraph()
# p1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# p.add_run('兹有' % _staff)
p1.space_after = Pt(25)
p1.add_run('        兹有 ').font.size = Pt(13)
run1 = p1.add_run(value[0])
run1.font.underline = True
run1.font.size = Pt(13)
p1.add_run(' 同志（性别： ').font.size = Pt(13)
run1 = p1.add_run(value[1])
run1.font.underline = True
run1.font.size = Pt(13)
p1.add_run(' , 身份证号码：').font.size = Pt(13)
run1 = p1.add_run(value[3])
run1.font.underline = True
run1.font.size = Pt(13)
p1.add_run('） ,  自').font.size = Pt(13)
run1 = p1.add_run(value[4])
run1.font.underline = True
run1.font.size = Pt(13)

p1.add_run(' 至 ').font.size = Pt(13)
run1 = p1.add_run(value[5])
run1.font.underline = True
run1.font.size = Pt(13)

p1.add_run('，在我公司任 ').font.size = Pt(13)
run1 = p1.add_run(value[2])
run1.font.underline = True
run1.font.size = Pt(13)
p1.add_run(' 职务。').font.size = Pt(13)

p2 = document.add_paragraph()
p2.add_run('        现因 ').font.size = Pt(13)
reason = input('请输入原因：')
run2 = p2.add_run('%s' % reason)
run2.font.underline = True
run2.font.size = Pt(13)
p2.add_run(' 向公司提出离职，于 ').font.size = Pt(13)
run2 = p2.add_run(value[5])
run2.font.underline = True
run2.font.size = Pt(13)
p2.add_run(' 日正式与公司解除劳动关系。 在此工作期间').font.size = Pt(13)
nowTime = time.localtime()
year = input('请输入年度时间（默认当前年 格式：2021）:') or time.strftime("%Y", nowTime)
run2 = p2.add_run('%s' % year)
run2.font.underline = True
run2.font.size = Pt(13)
p2.add_run(' 年年假已使用 ').font.size = Pt(13)
day = input('请输入年假使用天数:(默认0)') or "0"
run2 = p2.add_run('%s' % day)
run2.font.underline = True
run2.font.size = Pt(13)
p2.add_run(' 天，社保公积金缴纳至 ').font.size = Pt(13)
year1 = input('请输社保公积金缴纳最后时间（格式：2021-02）:')
run2 = p2.add_run('%s' % year1)
run2.font.underline = True
run2.font.size = Pt(13)
p2.add_run(' 月。').font.size = Pt(13)
p3 = document.add_paragraph()
p3.add_run('     特此证明!').font.size = Pt(13)
p3.space_after = Pt(35)

p4 = document.add_paragraph()
p4.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
p4.add_run(' 单位（盖章）：').font.size = Pt(13)
company = input('请输公司名称:')
run3 = p4.add_run('%s' % company)
run3.font.underline = True
run3.font.size = Pt(13)

p5 = document.add_paragraph()
p5.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
run4 = p5.add_run(value[5])
run4.font.underline = True
run4.font.size = Pt(13)

p6 = document.add_paragraph()
p6 = document.add_paragraph()
p6 = document.add_paragraph()
p6 = document.add_paragraph()
p6.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
acc = input('请输入公积金账号：默认无') or "无"
p6.add_run('公积金账号：').font.size = Pt(11)
run5 = p6.add_run('%s' % acc)
run5.font.underline = True
run5.font.size = Pt(11)
p6 = document.add_paragraph()
p6.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
p6.add_run('*温馨提示：离职证明只开一次，请您妥善保管，遗失、损毁自行负责。').font.size = Pt(11)

document.add_page_break()
document.save(value[0] + '离职证明.docx')
